package cn.wolfcode.p2p.base.util;

import cn.wolfcode.p2p.base.exception.CustomerException;
import com.alibaba.druid.sql.visitor.functions.If;
import org.springframework.util.StringUtils;

/**
 * 断言工具类,做校验判断
 */
public enum AssertUtil {
    /**
     *
     */
    INSTANCE;

    public AssertUtil isNotNull(Object obj, String errorMsg) {
        if (obj == null) {
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    public AssertUtil isNotEmpty(String str, String errorMsg) {
        if (!StringUtils.hasLength(str)) {
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    public AssertUtil isPhoneNumber(String phoneNumber, String errorMsg) {
        String regex = "^1[3|4|5|6|7|8][0-9]{9}$";
        if (!phoneNumber.matches(regex)) {
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    public AssertUtil lengthIn(String str, int minLength, int maxLength, String errorMsg) {
        if (str.length() < minLength || str.length() > maxLength) {
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    public AssertUtil isFalse(boolean condition, String errorMsg) {
        if (condition) {
            throw new CustomerException(errorMsg);
        }
        return this;
    }

}
